

class Stack:

    # 初始化栈
    def __init__(self,size=100):
        self.stack=[]
        self.size=size
        self.top=-1

    # 判断栈是否为空
    def is_empty(self)->bool:
        return self.top==-1

    # 判断栈是否已满
    def is_full(self)->bool:
        return self.top==self.size-1

    # 插入元素
    def push(self,value)->None:
        if self.is_full():
            raise Exception('stack is full')
        self.stack.append(value)
        self.top+=1
        

    # 删除元素
    def pop(self):
        if self.is_empty():
            raise Exception('stack is empty')

        last_ele=self.stack.pop()
        self.top-=1
        return last_ele

    # 获取栈顶元素
    def peek(self):
        if self.is_empty():
            raise Exception('stack is empty')
        
        return self.stack[self.top]

    

if __name__ == '__main__':
    stack=Stack(4)
    while True:
        print('请输入操作：push/pop/peek')
        opr=input()
        try:
            if opr=='push':
                print('push的元素=?')
                push_ele=input()
                stack.push(push_ele)
            elif opr=='pop':
                pop_ele=stack.pop()
                print(f'pop ele={pop_ele}')
            elif opr=='peek':
                peek_ele=stack.peek()
                print(f'peek ele={peek_ele}')
        except Exception as e:
            print(e)


        
    